$(document).ready( function() {
	moduleVlc.initialize();
});

var moduleVlc = new ModuleVlc();

function ModuleVlc() {
	log( "[ModuleVlc]" );
}

ModuleVlc.prototype.initialize = function() {
	log( "[ModuleVlc.initialize]" );

	bindEvents();
	executeStatusCommand( "" );

	setInterval( function() {
		//var image = $("input[type=image]#refreshUserList");
		//toogleBusy( image );
		executeStatusCommand( "" );
	}, 10000 ); //10 seconds
}

//-------------------------------------------------//

var canExecuteCommand = true;
var debugActivated = true;

function bindEvents() {
	log( "[ModuleVlc.bindEvents]" );

	$("input[type=button]").live( 'click tap', function( event ) {
		event.preventDefault();

		var command = $(event.currentTarget).attr("command");
		executeStatusCommand( command );

		return false;
	});

	var $slide = $("<label id='slide'/>").css({ position : 'absolute', top : -25, left : 0 }).hide();
	$( "#slider" ).slider({
		range: "min",
		highlight: true,
		animate: "slow",
		create: function( event, ui ) {
			createSlider( event, ui );
		},
		start: function( event, ui ) {
			startSlider( event, ui );
		},
		slide: function( event, ui ) {
			slideSlider( event, ui, $slide );
		},
		change: function( event, ui ) { 
			changeSlider( event, ui, $slide );
		},
		stop: function( event, ui ) { 
			stopSlider( event, ui );
		}
	}).find( ".ui-slider-handle" ).append( $slide ).hover( function(){ $slide.show() }, function(){ $slide.hide() });
}

function executeStatusCommand( command ) {
	log( "[ModuleVlc.executeStatusCommand]" );

	if( canExecuteCommand ) {

		$.getJSON( "/vlc/rest/command/" + command, function( response ) {

			if( hasCompleteResponse( response ) ) {

				var data = response.data;

				log( data.information.category.meta.filename );
				log( data.fullscreen );

				//update play/pause label
				var play_pause_label = "Pause";
				if( data.state == "paused" || data.state == "stopped" ) {
					play_pause_label = "Play";
				}
				$("input[type=button]").filter( function( index ) {
					return $(this).attr("command") == "pl_pause";
				}).val( play_pause_label );

				//update slider
				$( "#slider" )
					.slider( "option", "min", 0 )
					.slider( "option", "max", data.length )
					.slider( "option", "value", data.time );

				//update times
				$( "#time" )
					.html( "<label>" + parseSecondsToStringDate( data.time ) + "</label>" +
							"<label class='right'>" + parseSecondsToStringDate( data.length ) + "</label>");
			}

		}).error( function( errorResponse ) {
			showErrorMessage( errorResponse.statusText );
		});
	}
}

//in seconds
function parseSecondsToStringDate( time ) {
	var date = new Date( time * 1000 );
	var hours = ( date.getUTCHours() < 10 ) ? "0" + date.getUTCHours() : date.getUTCHours();
	var minutes = ( date.getUTCMinutes() < 10 ) ? "0" + date.getUTCMinutes() : date.getUTCMinutes();
	var seconds = ( date.getUTCSeconds() < 10 ) ? "0" + date.getUTCSeconds() : date.getUTCSeconds();
	return hours + ":" + minutes + ":" + seconds;
}

function createSlider( event, ui ) {
	//do nothing
}
function startSlider( event, ui ) {
	canExecuteCommand = false;
}
function slideSlider( event, ui, slide ) {
	slide.text( parseSecondsToStringDate( ui.value ) ).show();
}
function changeSlider( event, ui, slide ) {
	slide.hide();
}
function stopSlider( event, ui ) {
	canExecuteCommand = true;
	executeStatusCommand( "seek&val=" + ui.value );
}

//Common functions

function toogleBusy( image ) {
	log( "[ModuleVlc.toogleBusy]" );

	$(image).attr( 'src', '/vlc/images/spinner.gif' );
}

function hasCompleteResponse( response ) {
	log( "[ModuleVlc.hasCompleteResponse]" );

	if( response.errorMsg != null ) {
		showErrorMessage( response.errorMsg )
		return false
	}
	return response.data != null;
}

function showErrorMessage( message ) {
	log( "[ModuleVlc.showErrorMessage]" );
	log( message );
	$("#error").text( message ).slideDown( 900 ).delay( 2400 ).slideUp( 900 );
}

function log( message ) {
	if( debugActivated && message != null ) {
		console.log( message );
	}
}
